﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Strings Exercise 7</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Script that parses an URL address given in the format.</h1>
    <label for="str-input">String:</label>
    <input type="text" id="str-input" />
    <button onclick="execute()">execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function parseURL(url)
        {
            var protocol = "";
            var server = url;
            var resource = "";
            var protocolEndIdx = url.indexOf("://");

            // Extract protocol if available
            if (protocolEndIdx >= 0)
            {
                protocol = url.substring(0, protocolEndIdx);
                server = url.substring(protocolEndIdx + 3, url.length);
            }

            // Extract server and recourse if available
            var serverEndIdx = server.indexOf("/");
            if (serverEndIdx >= 0)
            {
                resource = server.substring(serverEndIdx, url.length);
                server = server.substring(0, serverEndIdx);
            }

            return {
                protocol: protocol,
                server: server,
                resource: resource,
                toString: function() {
                    return "protocol: " + this.protocol + " server: " + this.server + " resource: " + this.resource;
                }
            }
        }

        function execute()
        {
            var inpStr = jsConsole.read("#str-input");

            var urlObj = parseURL(inpStr);

            jsConsole.writeLine(urlObj);
        }
    </script>
</body>
</html>
